package com.hosp.controller.v1;

import java.util.Objects;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.alibaba.druid.util.StringUtils;
import com.hosp.bean.Sysuser;
import com.hosp.controller.dto.UserDTO;
import com.hosp.service.SysLoginService;
import com.hosp.service.UserService;
import com.hosp.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 系统登录 API
 *
 * @author ztranscript 2021/9/11 10:00
 **/
@RestController
@RequestMapping("/oauth")
public class SysLoginController {
    @Autowired
    private SysLoginService loginService;

    @Autowired
    private UserService userService;

    /**
     * 登录认证
     *
     * @param loginUser 用户名密码
     * @param verify    验证码
     * @param request   request
     * @return 响应
     */
    @PostMapping("/login")
    public ResultVO<Sysuser> login(Sysuser loginUser, String verify, HttpServletRequest request) {
        // 1.校验验证码
        String code = (String) request.getSession().getAttribute("verify");
        /*System.out.println("code = " + code);*/
        if (Objects.isNull(code)) {
            return new ResultVO<>(500, "验证码已失效,请刷新");
        }
        if (!StringUtils.equals(verify, code)) {
            return new ResultVO<>(500, "验证码错误");
        }

        // 2.用户信息认证
        Sysuser dbUser = loginService.login(loginUser);

        ResultVO<Sysuser> resultVO = new ResultVO<>();
        if (Objects.isNull(dbUser)) {
            resultVO.setCode(500);
            resultVO.setMsg("用户名或密码错误");
        } else if (dbUser.getuState() == 1) {
            resultVO.setCode(500);
            resultVO.setMsg("此用户被禁用,请联系管理员");
        } else {
            resultVO.setMsg("认证通过");
            resultVO.setObj(dbUser);
            // 将用户信息存入 session, 30 min 后过期
            HttpSession session = request.getSession();
            session.setAttribute("userInfo", dbUser);
            session.setMaxInactiveInterval(30 * 60);
        }

        return resultVO;
    }

    /**
     * 退出
     *
     * @param request request
     * @return 响应
     */
    @GetMapping("/logout")
    public ResultVO<?> login(HttpServletRequest request) {
        // 清除用户登录信息
        request.getSession().removeAttribute("userInfo");
        return new ResultVO<>();
    }

    /**
     * 刷新验证码
     *
     * @param request request
     * @return 响应
     */
    @GetMapping("/verify")
    public ResultVO<String> verify(HttpServletRequest request) {
        // 生成随机验证码
        long uuid = UUID.randomUUID().getMostSignificantBits();
        uuid = Math.abs(uuid);
        String verify = String.valueOf(uuid).substring(0, 6);
        // 存入 Session, 60s 后过期
        HttpSession session = request.getSession();
        session.setAttribute("verify", verify);
        session.setMaxInactiveInterval(60);

        return new ResultVO<>(verify);
    }

    /**
     * 获取当前登录用户信息
     *
     * @param request request
     * @return 响应
     */
    @GetMapping("/user-info")
    public ResultVO<UserDTO> userInfo(HttpServletRequest request) {
        Sysuser orgUser = (Sysuser) request.getSession().getAttribute("userInfo");
        // 获取用户详细信息
        UserDTO userDTO = userService.selectById(orgUser.getuId());

        return new ResultVO<>(userDTO);
    }

}
